package mskim.study.ooad.ch01_06;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class Inventory {
	private List<Instrument> inventory;

	public Inventory() {
		inventory = new ArrayList<Instrument>();
	}
	
	public void addInstrument(String serialNumber, double price, InstrumentSpec spec){
		Instrument instrument=null;
		if (spec instanceof GuitarSpec){
			instrument = new Guitar(serialNumber, price, (GuitarSpec) spec);
		}else{
			instrument = new Mandolin(serialNumber, price, (MandolinSpec) spec);
		}
		inventory.add(instrument);
	}
	
	public Instrument getInstrument(String serialNumber){
		for(Iterator<Instrument> i=inventory.iterator(); i.hasNext(); ){
			Instrument instrument = i.next();
			if (instrument.getSerialNumber().equals(serialNumber)){
				return instrument;
			}
		}
		return null;
	}
	
	public List<Instrument> search(InstrumentSpec searchSpec){
		List<Instrument> matchingInstruments = new ArrayList<Instrument>();
		
		for (Iterator<Instrument> i = inventory.iterator(); i.hasNext(); ) {
			Instrument instrument = i.next();
			if (instrument instanceof Mandolin){
				Mandolin mandolin = (Mandolin) instrument;
				if ( mandolin.getSpec().matchs(searchSpec) )
					matchingInstruments.add(mandolin);
			}else{
				Guitar guitar = (Guitar) instrument;
				if (guitar.getSpec().matchs(searchSpec))
					matchingInstruments.add(guitar);
			}
		}
		return matchingInstruments;
	}
}
